//
//  YAYIEditNameViewController.m
//  yayi
//
//  Created by Veer on 16/3/18.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIFeedbackViewController.h"
#import "NSString+Tools.h"

@interface YAYIFeedbackViewController () <UITextViewDelegate>

@property (nonatomic, weak) UITextView *editTextView;
@property (strong,nonatomic) UIButton *backBtn;

@property (nonatomic, weak) UILabel *placeholderLbl;

@end

@implementation YAYIFeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor colorWithRed:0.937255 green:0.937255 blue:0.956863 alpha:1.0];
    
    //
    [self setupNav];
    [self createBackButton];
    
    [self setupSubView];
    
}

- (void)setupNav
{
    // left button
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:@"提交" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    
    rightBtn.bounds = CGRectMake(0, 0, 40, 20);
    [rightBtn addTarget:self action:@selector(saveClick:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
}


-(void)createBackButton{
    if (self.backBtn == nil) {
        self.backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.backBtn setBackgroundImage:[UIImage  imageNamed:@"back_button"] forState:UIControlStateNormal];
        self.backBtn.frame = CGRectMake(0, 0, 11, 20);
        [self.backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *item = [[UIBarButtonItem  alloc] initWithCustomView:self.backBtn];
        self.navigationItem.leftBarButtonItem = item;
    }
}

- (void)setupSubView
{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    view.frame = CGRectMake(0, 20, SCREEN_width, 150);
    // 创建textview
    UITextView *textView = [[UITextView alloc] init];
    textView.font = [UIFont systemFontOfSize:15];
    textView.delegate = self;
    
    textView.frame = CGRectMake(10, 20, SCREEN_width - 20, 150);
    self.editTextView = textView;
    [self.view addSubview:textView];

    
    NSString *placeholderStr = @"欢迎向我们吐槽雅同的各种问题。（200字以内）";
    UILabel *placeholderLbl = [[UILabel alloc] init];
    placeholderLbl.numberOfLines = 0;
    placeholderLbl.textColor = YAYIColor(146, 146, 146);
    placeholderLbl.font = [UIFont systemFontOfSize:15];
    placeholderLbl.text = placeholderStr;

//    textView.text
    CGSize placeholderSize = [placeholderStr sizeWithFont:placeholderLbl.font constrainedToSize:CGSizeMake(textView.frame.size.width - 5, MAXFLOAT)];
    
    [textView addSubview:placeholderLbl];
    self.placeholderLbl = placeholderLbl;
    placeholderLbl.frame = CGRectMake(5, 8, placeholderSize.width, placeholderSize.height);
    
    
    UIView *lineVw = [[UIView alloc] init];
    lineVw.backgroundColor = YAYIColor(224, 217, 217);
    [self.view addSubview:lineVw];
    [lineVw mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@0.5);
        make.width.equalTo(self.view);
        make.bottom.equalTo(textView.mas_top);
    }];
    
    UIView *lineVw2 = [[UIView alloc] init];
    lineVw2.backgroundColor = YAYIColor(224, 217, 217);
    [self.view addSubview:lineVw2];
    [lineVw2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@0.5);
        make.width.equalTo(self.view);
        make.bottom.equalTo(textView.mas_bottom);
    }];
}


#pragma mark 按钮相应方法
- (void)saveClick:(UIButton *)sender
{
    YAYILog(@"rect:%@", NSStringFromCGRect(self.view.frame));
    
    
    [self.editTextView resignFirstResponder];
    [self showHubWith:@"提交成功" isBack:YES];
    
    //[self backAction];
}


-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark - 工具方法
- (void)showHubWith:(NSString *)messageStr isBack:(BOOL)isBack
{
    YAYIHudProgressView *hud = [[YAYIHudProgressView alloc] initWithView:self.view.window];
    [hud setMessage:messageStr];
    [self.view.window addSubview:hud];
    [hud removeFromSuperview:YES time:1.0 isHinden:YES];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        if (isBack) {
            [self backAction];
        }
        
    });
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length > 0) {
        self.placeholderLbl.hidden = YES;
    }else{
        self.placeholderLbl.hidden = NO;
    }
}



@end
